﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XHFrame
{
    public class BaseModule
    {
        /// <summary>
        /// 注册
        /// </summary>
        public enum EnumRegisterMode
        {
            /// <summary>
            /// 不进行注册
            /// </summary>
            NotRegister,
            /// <summary>
            /// 自动注册
            /// </summary>
            AutoRegister,
            /// <summary>
            /// 已经注册
            /// </summary>
            AlreadyRegister
        }

        private EnumObjectState objectState = EnumObjectState.Initial;
        /// <summary>
        /// 状态属性改变
        /// </summary>
        public EnumObjectState ObjectState
        {
            get
            {
                return objectState;
            }

            set
            {
                if (objectState == value) return;
                EnumObjectState oldState = objectState;
                objectState = value;
                if (null != StateChange)
                {
                    StateChange(this, objectState, oldState);
                }
                OnStateChange(objectState, oldState);
            }
        }

        /// <summary>
        /// 状态改变事件
        /// </summary>
        public event StateChangedEvent StateChange;

        /// <summary>
        /// 状态改变事件(继承后重写可以获取改变)
        /// </summary>
        /// <param name="NewState"></param>
        /// <param name="oldState"></param>
        protected virtual void OnStateChange(EnumObjectState NewState, EnumObjectState oldState)
        {

        }


        /// <summary>
        /// 注册Mode
        /// </summary>
        private EnumRegisterMode registerMode = EnumRegisterMode.NotRegister;

        /// <summary>
        /// 自动注册属性
        /// </summary>
        public bool AutoRegister
        {
            get
            {
                return registerMode == EnumRegisterMode.NotRegister ? false : true;
            }
            set
            {
                if (registerMode == EnumRegisterMode.NotRegister || registerMode == EnumRegisterMode.AutoRegister)
                {
                    registerMode = value ? EnumRegisterMode.AutoRegister : EnumRegisterMode.NotRegister;
                }
            }
        }

        /// <summary>
        /// 是否已经注册
        /// </summary>
        public bool HasRegister
        {
            get
            {
                return registerMode == EnumRegisterMode.AlreadyRegister;
            }
        }

        /// <summary>
        /// 加载
        /// </summary>
        public void Load()
        {
            if (objectState != EnumObjectState.Initial) return;

            objectState = EnumObjectState.Loading;

            if (registerMode == EnumRegisterMode.AutoRegister)
            {
                // register
                ModuleManager.Instance.Register(this);
                registerMode = EnumRegisterMode.AutoRegister;
            }

            OnLoad();
            objectState = EnumObjectState.Ready;
        }

        /// <summary>
        /// 加载事件
        /// </summary>
        protected virtual void OnLoad()
        {

        }

        /// <summary>
        /// 释放
        /// </summary>
        public void Release()
        {
            if (objectState != EnumObjectState.Disabled)
            {
                objectState = EnumObjectState.Disabled;

                if (registerMode == EnumRegisterMode.AlreadyRegister)
                {
                    // unregister
                    ModuleManager.Instance.UnRegister(this);
                    registerMode = EnumRegisterMode.AutoRegister;
                }

                OnRelease();
            }
        }

        /// <summary>
        /// 释放事件
        /// </summary>
        protected virtual void OnRelease()
        {

        }
    }
}
